package com.prb.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;


public class JsonUtils {

    public static <T> T readJsonFromFile(String fileName, Class<T> clazz) throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();
        URL url = JsonUtils.class.getClassLoader().getResource(fileName);

        return objectMapper.readValue(url , clazz);

    }


    public static <T> T readJsonFromText(String text, Class clazz) throws Exception {

        ObjectMapper objectMapper = new ObjectMapper();

        return (T) objectMapper.readValue(text , clazz);


    }

    public static <T> List<T> jsonToListFromFile(String fileName, Class<T> clazz) throws Exception  {

        ObjectMapper om = new ObjectMapper();

        InputStream resourceAsStream = JsonUtils.class.getClassLoader().getResourceAsStream(fileName);

        return  om.readValue(resourceAsStream, List.class);
    }


    public static <T> List<T> jsonToListFromText(String text, Class<T> clazz) throws Exception {

        ObjectMapper om = new ObjectMapper();

        return  om.readValue(text , List.class);
    }

    public static Map<String, Object> readJsonFromFile(String fileName) throws IOException {

        ObjectMapper om = new ObjectMapper();

        InputStream resourceAsStream = JsonUtils.class.getClassLoader().getResourceAsStream(fileName);

        return  om.readValue(resourceAsStream, Map.class);


    }


}
